﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Coderuse.BalanceCalculator
{
    class Transaction
    {
        public int trnDay;
        public Decimal trnAmount;
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Calculate Average Balance";
            int month = 0;
            string input = string.Empty;
            do
            {
                Console.Write("Month: ");
                input = Console.ReadLine();
                if (!int.TryParse(input, out month))
                    Console.WriteLine("Correctly enter month number.");
                Console.WriteLine();
            } while (month <= 0 || month > 12);

            int year = 0;
            do
            {
                Console.Write("Year: ");
                input = Console.ReadLine();
                if (!int.TryParse(input, out year))
                    Console.WriteLine("Correctly enter Year number.");
                Console.WriteLine();
            } while (year <= 0 || year > DateTime.Today.Year);

            int daysInMonth = DateTime.DaysInMonth(year, month);

            Console.WriteLine("//////// Enter Transactions Serially ////////");
            int enteredDay = 0;
            int lastEnteredDay = 0;
            decimal trnAmount = 0.00m;
            List<Transaction> transactions = new List<Transaction>();
            while (true)
            {
                do
                {
                    Console.Write("Day: ");
                    input = Console.ReadLine();
                    if (!int.TryParse(input, out enteredDay))
                        Console.WriteLine("Correctly enter Day number.");
                    Console.WriteLine();
                } while (enteredDay <= 0);
                if (enteredDay < lastEnteredDay || enteredDay > daysInMonth)
                {
                    Console.Write("You have entered a wrong date. Do you want to continue (y/n)? ");
                    input = Console.ReadLine();
                    if (input.Equals("y") || input.Equals("Y"))
                    {
                        Console.WriteLine("\n");
                        continue;
                    }
                    else
                        break;
                }

                lastEnteredDay = enteredDay;

                do
                {
                    Console.Write("Amount: ");
                    input = Console.ReadLine();
                    if (!decimal.TryParse(input, out trnAmount))
                        Console.WriteLine("Correctly enter exact amount.");
                    Console.WriteLine();
                } while (trnAmount == 0.00m);

                transactions.Add(new Transaction() { trnDay = enteredDay, trnAmount = trnAmount });

            }

            int activeDays = 0;
            decimal grossTotal = 0.00m;
            Transaction lastTransaction = null;
            if (transactions.Count > 0)
            {
                foreach (Transaction t in transactions)
                {
                    if (null != lastTransaction)
                    {
                        if (t.trnDay > lastTransaction.trnDay)
                            activeDays = t.trnDay - lastTransaction.trnDay;
                        else
                        {
                            lastTransaction.trnAmount += t.trnAmount;
                            continue;
                        }
                    }
                    else
                    {
                        lastTransaction = t;
                        continue;
                    }
                    grossTotal += activeDays * lastTransaction.trnAmount;

                    lastTransaction.trnAmount += t.trnAmount;
                    lastTransaction.trnDay = t.trnDay;
                }
                grossTotal += (daysInMonth - lastTransaction.trnDay + 1) * lastTransaction.trnAmount;
            }

            Console.WriteLine("\nAverage daily balance: {0:C}", grossTotal / daysInMonth);

            Console.ReadLine();
        }
    }
}
